package com.smile.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.model.dto.request.AddRoleRequestDTO;
import com.smile.model.dto.request.EditRoleRequestDto;
import com.smile.model.dto.request.GetRoleListRequestDTO;
import com.smile.model.response.response.GetRoleListResponseDTO;
import com.smile.model.response.response.GetStartingRoleResponseDTO;
import com.smile.model.request.AddRoleRequest;
import com.smile.model.request.EditRoleRequest;
import com.smile.model.request.GetRoleByIdRequest;
import com.smile.model.request.GetRoleListRequest;
import com.smile.model.response.GetRoleByIdResponse;
import com.smile.model.response.GetRoleListResponse;
import com.smile.model.response.GetStartingRoleResponse;
import com.smile.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 角色相关controller
 *
 * @author 宁海博
 * @Date 2025 6/17
 */
@RestController
@RequestMapping("/role")
@Validated
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 添加角色
     *
     * @param addRoleRequest
     * @return
     */
//    @SaCheckPermission("角色管理")
    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> addRole(@RequestBody @Valid AddRoleRequest addRoleRequest) {

        AddRoleRequestDTO addRoleRequestDTO = new AddRoleRequestDTO();
        BeanUtil.copyProperties(addRoleRequest, addRoleRequestDTO);
        Boolean b = roleService.addRole(addRoleRequestDTO);
        if (b) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    /**
     * 获取已开启的角色
     *
     * @return
     */
    @PostMapping(value = "/getStartingRole")
    public R<List<GetStartingRoleResponse>> getStartingRole() {
        List<GetStartingRoleResponseDTO> getStartingRoleResponseDTOS = roleService.getStartingRole();
        return R.ok(BeanUtil.copyToList(getStartingRoleResponseDTOS, GetStartingRoleResponse.class));
    }

    /**
     * 根据id查询角色
     *
     * @param getRoleByIdRequest
     * @return
     */
    @PostMapping(value = "/getRoleById", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<GetRoleByIdResponse> getRoleById(@RequestBody GetRoleByIdRequest getRoleByIdRequest) {
        GetRoleListResponseDTO getRoleListResponseDTO = roleService.getRoleById(getRoleByIdRequest.getId());
        GetRoleByIdResponse getRoleByIdResponse = new GetRoleByIdResponse();
        BeanUtil.copyProperties(getRoleListResponseDTO, getRoleByIdResponse);
        return R.ok(getRoleByIdResponse);
    }


    /**
     * 根据id查询角色
     *
     * @return
     */
    @PostMapping(value = "/getRoleById_new", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<GetRoleByIdResponse> getRoleById_new(@RequestBody List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            throw new RuntimeException("传入的参数为空!");
        }
        Long id = ids.stream().findFirst().orElse(null);


        GetRoleListResponseDTO getRoleListResponseDTO = roleService.getRoleById(id);
        GetRoleByIdResponse getRoleByIdResponse = new GetRoleByIdResponse();
        BeanUtil.copyProperties(getRoleListResponseDTO, getRoleByIdResponse);
        return R.ok(getRoleByIdResponse);
    }

    /**
     * 获取角色列表
     *
     * @param getRoleListRequest
     * @return
     */
//    @SaCheckPermission("角色管理")
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Page<GetRoleListResponse>> listRole(@RequestBody GetRoleListRequest getRoleListRequest, HttpServletRequest request) {
        System.out.println("getRoleListRequest=" + getRoleListRequest);
        GetRoleListRequestDTO getRoleListRequestDTO = new GetRoleListRequestDTO();
        BeanUtil.copyProperties(getRoleListRequest, getRoleListRequestDTO);
        roleService.getRoleList(getRoleListRequestDTO);

        Page<GetRoleListResponseDTO> roleList = roleService.getRoleList(getRoleListRequestDTO);
        System.out.println("/role/list返回的数据为:" + roleList.getRecords());
        return R.ok(MyBatisPlusPageConverter.convertPage(roleList, GetRoleListResponse.class));
    }

    /**
     * 编辑角色
     *
     * @param editRoleRequest
     * @return
     */
//    @SaCheckPermission("角色管理")
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> editRole(@RequestBody @Valid EditRoleRequest editRoleRequest) {
        EditRoleRequestDto editRoleRequestDto = new EditRoleRequestDto();
        BeanUtil.copyProperties(editRoleRequest, editRoleRequestDto);
        Boolean result = roleService.editRole(editRoleRequestDto);
        if (result) {
            return R.ok();
        } else {
            return R.fail();
        }
    }

    /**
     * 删除角色
     *
     * @param id
     * @return
     */
//    @SaCheckPermission("角色管理")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public R<Void> deleteRole(@RequestBody(required = true) Long id) {
        Boolean result = roleService.deleteRole(id);
        if (result) {

            return R.ok();
        } else {
            return R.fail("已有账号使用该角色，请修改账号角色后再删除");
        }
    }


}
